﻿

// 跳转到搜索页面
function gotoSearchPage(keyword) {
    window.location.href = RSECGlobal.SearchSite + "Goods/Search.aspx?KeyWord=" + escape(keyword);
}

// 顶部搜索条
function topSearch() {
    $(window).scroll(function () {
        // 判断头部广告条是否显示,如果显示着,则不出现顶部搜索条
        var $js_ads_banner_top = $('#js_ads_banner_top');
        if ($js_ads_banner_top.length > 0 && $js_ads_banner_top.is(':visible')) return;

        var scrollTop = $(window).scrollTop();//鼠标滚动的距离
        if (scrollTop > 250) {
            $('#topSearch').css('top', '0px');
            $('.topSearch').css('top', (scrollTop + 40) + 'px');
        } else {
            $('#topSearch').css('top', '');
            $('.topSearch').hide();
        }
    });
}

// 图片懒加载
function loadlazy() {
    $("img.lazyImage").lazyload({
        threshold: 200,
        skip_invisible: false,
        placeholder: RSECGlobal.DefaultSite + 'images/loading.gif'
    });
    $("img.lazyImage1").lazyload({
        threshold: 200,
        skip_invisible: false,
        placeholder: ''
    });
}
// 搜索
function search() {
    // 自动完成远程地址
    var acUrl = RSECGlobal.DefaultSite + "Goods/KeySearch.ashx?Action=SearchKey";
    // 自动完成配置
    var acOptions = {
        minChars: 1,
        max: 10,
        width: 316,
        scroll: false,
        autoHide: true,
        selectFirst: false,
        dataType: 'json',
        parse: function (data) {
            return $.map(data, function (row) {
                return {
                    data: row,
                    value: row,
                    result: row
                }
            });
        },
        formatItem: function (row, i, n) {
            return row;
            //return "<table width='100%'><tr><td align='left'>" + row + "</td><td align='right'><font style='color:#999;'>约" + row + "个宝贝</font>&nbsp;&nbsp;</td></tr></table>";
        }
    };

    // 头部搜索
    $('#BtnSearch').click(function (event) {
        var data = $('#searchKeyWord').val();
        if (data.length == 0) return;
        gotoSearchPage(data);
    });

    acOptions['resultsClass'] = 'ac_results';
    acOptions['width'] = 316;
    $('#searchKeyWord').autocomplete(acUrl, acOptions)
        .result(function (event, data, formatted) {
            gotoSearchPage(data);
        });
    $('#searchKeyWord').keydown(function (event) {
        if (event.keyCode == 13) {
            $('#BtnSearch').click();
        }
    });

    // 顶部搜索栏
    acOptions['resultsClass'] = 'ac_results topSearch';
    acOptions['width'] = 600;
    $('#topSearchKey').autocomplete(acUrl, acOptions)
        .result(function (event, data, formatted) {
            gotoSearchPage(data);
        });
    $('#topSearchKey').keydown(function (event) {
        if (event.keyCode == 13) {
            $('#BtnTopSearch').click();
        }
    });
    $('#BtnTopSearch').click(function (event) {
        var data = $('#topSearchKey').val();
        if (data.length == 0) return;
        gotoSearchPage(data);
    });
}

// 获取购物车数量
function GetBasketItemQty() {
    var memberUrl = RSECGlobal.MemberSite;
    if ('https:' == document.location.protocol)
        memberUrl = RSECGlobal.MemberSite.replace("http:", "https:");
    $.ajax({
        type: "POST", //访问WebService使用Post方式请求
        contentType: "application/json;charset=utf-8", //WebService 会返回Json类型
        url: RSECGlobal.DefaultSite + "Customer/ConsumerWebServices.asmx/GetBasketItemQty", //调用WebService
        data: "{}", //Email参数
        dataType: 'json',
        beforeSend: function (x) { x.setRequestHeader("Content-Type", "application/json; charset=utf-8"); },
        error: function (x, e) { },
        success: function (response) { //回调函数，result，返回值
            $("#miniCartCount").html(response.d + " 件");
        }
    });
}

// 退出
function Logout() {
    jQuery.cookie('LoginInfoCookie', '', { path: '/', expires: -1 });
    location = RSECGlobal.MemberSite + 'Customer/ConsumerLogout.aspx';
	//location = "https://b2b.nbdeli.com/" + 'Customer/ConsumerLogout.aspx';
}

// 分类导航
function classNav() {
    if (isHomePage) {
        $('#allClass').show();
    } else {
        $('#allClass').css('top', '34px');
        $('.allClass').hover(function () {
            $('#allClass').show();
        }, function () {
            $('#allClass').hide();
        });
    }
    $('#allClass>ul>li').hover(function () {
        $(this).toggleClass('on');
        $(this).find('.sublist').each(function () {
            var that = $(this);
            var height = that.find('.sublist-body-left').height() + 20;
            that.height(height);
        });
    }, function () {
        $(this).toggleClass('on');
    });
}

$(function () {

    // 顶部搜索条
    topSearch();

    // 图片懒加载
    loadlazy();
    
    // 搜索
    search();

    // 分类导航
    classNav();

    // 获取购物车数量
    setTimeout(GetBasketItemQty, 200);
});